package com.chuangshu.chuangshuteam.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chuangshu.chuangshuteam.entity.Applications;
import com.chuangshu.chuangshuteam.entity.AssessmentPassLists;
import com.chuangshu.chuangshuteam.mapper.ApplicationsMapper;
import com.chuangshu.chuangshuteam.mapper.AssessmentPassListsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class AssessmentPassListsTask {
    @Autowired
    ApplicationsMapper applicationsMapper;
    @Autowired
    AssessmentPassListsMapper assessmentPassListsMapper;

    @Scheduled(cron = "0 0 10 * * ?")  // 单位为毫秒
    public void addAssessmentPassLists() {
        LambdaQueryWrapper<Applications> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Applications::getRound1Status, "pass")
                        .or()
                                .eq(Applications::getRound2Status, "pass")
                                        .or()
                                                .eq(Applications::getFinalStatus, "pass");
        List<Applications> applications = applicationsMapper.selectList(lambdaQueryWrapper);
        if (applications.isEmpty()){
            return;
        }
        for (Applications application : applications) {
            AssessmentPassLists assessmentPassList = new AssessmentPassLists();
            assessmentPassList.setRecruitmentCycleId(application.getRecruitmentCycleId());
            assessmentPassList.setDirection(application.getDirection());
            if (application.getRound1Status().equals("pass")){
                assessmentPassList.setRoundNumber(1);
                application.setRound1Status("complete");
            } else if (application.getRound2Status().equals("pass")) {
                assessmentPassList.setRoundNumber(2);
                application.setRound2Status("complete");
            } else if (application.getFinalStatus().equals("pass")){
                assessmentPassList.setRoundNumber(3);
                application.setFinalStatus("complete");
            }
            assessmentPassList.setApplicationId(application.getId());
            assessmentPassList.setPassPerson(application.getApplicantName());
            assessmentPassList.setAddedBy(1L);
            assessmentPassListsMapper.insert(assessmentPassList);
            applicationsMapper.updateById(application);
        }


    }
}
